<?php

/**
 * This file used fields that are highly specific to the eAtlas.
 */

function _eatlas_search_node_to_result($item, $keys) {
	// Render the node.
	$node = node_load($item->sid);
	$build = node_view($node, 'eatlas_search_result');
	if (isset($build['field_authors'])) {
		hide($build['field_authors']);
	}
	if (isset($build['field_project_leaders'])) {
		hide($build['field_project_leaders']);
	}
	if (isset($build['field_published_date'])) {
		hide($build['field_published_date']);
	}
	// Person node type
	if (isset($build['field_organisation'])) {
		hide($build['field_organisation']);
	}
	// Person node type
	if (isset($build['field_web_site'])) {
		hide($build['field_web_site']);
	}

	unset($build['#theme']);
	$node->rendered = drupal_render($build);

	// Fetch comments for snippet.
	$node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);

	$extra = module_invoke_all('node_search_result', $node);

	$authors_list = '';
	$authors = field_get_items('node', $node, 'field_authors');
	if ($authors) {
		foreach($authors as $author) {
			if ($authors_list) {
				$authors_list .= ', ';
			}
			$authors_list .= render(field_view_value('node', $node, 'field_authors', $author, 'eatlas_search_result'));
		}
	}
	$leaders = field_get_items('node', $node, 'field_project_leaders');
	if ($leaders) {
		foreach($leaders as $leader) {
			if ($authors_list) {
				$authors_list .= ', ';
			}
			$authors_list .= render(field_view_value('node', $node, 'field_project_leaders', $leader, 'eatlas_search_result'));
		}
	}

	// Add the published date to the extra field, using the display defined for "search_result" in "Structure > Content types > XXX > Manage Display > Search result"
	$published_field = field_get_items('node', $node, 'field_published_date');
	if ($published_field) {
		$published_value = field_view_value('node', $node, 'field_published_date', $published_field[0], 'eatlas_search_result');
		$extra[] = render($published_value);
	}

	$link = NULL;
	if ($node->type === 'eatlas_georss_item') {
		$rss_link_field = field_get_items('node', $node, 'field_ea_rss_link');
		if ($rss_link_field) {
			$rss_link_output = field_view_value('node', $node, 'field_ea_rss_link', $rss_link_field[0]);

			// Adjust the search result link
			$link = render($rss_link_output);
		}
	} else {
		$uri = entity_uri('node', $node);
		$link = url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE)));
	}
	return array(
		'link' => $link,
		'type' => check_plain(node_type_get_name($node)),
		'title' => $node->title,
		//'user' => theme('username', array('account' => $node)),
		'user' => $authors_list,
		//'date' => $node->changed, // Required a timestamp - display mode not configurable (better use extra field)
		'node' => $node,
		'extra' => $extra,
		'score' => $item->calculated_score,
		'snippet' => _eatlas_search_excerpt($keys, $node->rendered),
		'language' => entity_language('node', $node)
	);
}

/**
 * Implements hook_node_update().
 * Triggered when a node is modified.
 * Used to flag index item for reindexation when a node is modified.
 * The re-indexation is done with the cron (run automatically or manually)
 */
function eatlas_search_node_update($node) {
	db_update('search_dataset')
		->fields(array('reindex' => REQUEST_TIME))
		->condition('sid', $node->nid)
		->condition('type', EATLAS_SEARCH_INDEX_NODE, '=')
		->execute();
}
// hook_node_delete($node)
function eatlas_search_node_delete($node) {
	_eatlas_search_delete_index($node->nid, EATLAS_SEARCH_INDEX_NODE);
}



/**************
 * INDEXATION *
 **************/



function _eatlas_search_nodes_status() {
	$total = db_query('SELECT COUNT(*) FROM {node}')->fetchField();
	$remaining = db_query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = '".EATLAS_SEARCH_INDEX_NODE."' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0")->fetchField();
	return array('remaining' => $remaining, 'total' => $total);
}



// Update node indexes - index ALL nodes whatever it's type, the search filter the type
function _eatlas_search_node_update_index($limit) {
	$result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = '".EATLAS_SEARCH_INDEX_NODE."' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit, array(), array('target' => 'slave'));

	foreach ($result as $node) {
		_eatlas_search_index_node($node);
	}
}
function _eatlas_search_index_node($node) {
	$node = node_load($node->nid);

	// Save the changed time of the most recent indexed node, for the search
	// results half-life calculation.
	variable_set('node_cron_last', $node->changed);

	// Render the node.
	$build = node_view($node, 'eatlas_search_index');
	unset($build['#theme']);
	$node->rendered = drupal_render($build);

	// H1 as higher priority in the index (when doing a search)
	$text = '<h1>' . check_plain($node->title) . '</h1>' . $node->rendered;

	// Fetch extra data normally not visible
	$extra = module_invoke_all('node_update_index', $node);
	foreach ($extra as $t) {
		$text .= $t;
	}

	// Update index
	_eatlas_search_index($node->nid, EATLAS_SEARCH_INDEX_NODE, $text);
}

?>
